const CommentModel = require("../model/Comment");

async function handleCreateNote(note) {
  const res = await CommentModel.create(note);
  if (res) {
    return res;
  }
}

async function handleCommentListOne(username) {
  const res = await CommentModel.findOne({ username }).sort({ _id: -1 });
  return res;
}

async function handleCommentListAll() {
  const res = await CommentModel.find({}).sort({ _id: -1 });
  return res;
}

async function handleDeleteList(username, _id) {
  await CommentModel.remove({
    username,
    _id,
  });
}

async function handleUpdateList(_id, username, content) {
  const res = await CommentModel.findOneAndUpdate(
    {
      _id,
      username,
    },
    { content },
    { new: true }
  );
  return res;
}

module.exports = {
  handleCreateNote,
  handleCommentListOne,
  handleCommentListAll,
  handleDeleteList,
  handleUpdateList,
};
